package com.book.service.impl;

import com.book.entity.BookOrder;
import com.book.entity.BookOrderItem;
import com.book.mapper.BookOrderItemMapper;
import com.book.mapper.BookOrderMapper;
import com.book.service.BookOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author SuperYu  2024/8/5
 */
@Service
public class BookOrderServiceImp implements BookOrderService {

    @Resource
    BookOrderMapper bookOrderMapper;

    @Resource
    BookOrderItemMapper bookOrderItemMapper;


    @Override
    @Transactional
    public int addOrder(BookOrder order, ArrayList<BookOrderItem> list) {
        System.out.println("新增的订单："+order);
        for (BookOrderItem item : list) {
            System.out.println("对应的订单详情："+item);
        }

        order.setCreateTime(new Date());
        // 1: 新增一条记录到订单表
        int i = bookOrderMapper.insertSelective(order);
        if(i > 0){
            // 2：再新增N条订单详情写到数据库的详情表
            return bookOrderItemMapper.insertMoreOrderItem(list);
        }


        throw new RuntimeException("订单添加失败");
    }
}
